package com.gomro.galaxy.foundation.bi.engine.util;

import cn.gomro.cloud.core.common.utils.DateUtils;
import com.gomro.galaxy.foundation.bi.engine.query.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author: zhoujian
 * @Date: 2020/4/26 9:48
 * @Version 1.0
 */
public class BoolQueryBuilders {
    public static BoolQueryBuilder organization(OrganizationQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getStart())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").gte(DateFormatUtils.format(query.getStart(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getEnd())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").lt(DateFormatUtils.format(query.getEnd(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getVirtual())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("virtual", query.getVirtual()));
        }
        if (Objects.nonNull(query.getArchitectureType())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("architectureType", query.getArchitectureType()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder quotationGoods(QuotationGoodsQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getStart())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").gte(DateFormatUtils.format(query.getStart(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getEnd())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").lt(DateFormatUtils.format(query.getEnd(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (!CollectionUtils.isEmpty(query.getVendorIds())) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("vendorOrganizationId", query.getVendorIds()));
        } else if (Objects.nonNull(query.getVendorId())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("vendorOrganizationId", query.getVendorId()));
        }
        if (!CollectionUtils.isEmpty(query.getMemberIds())) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("memberId", query.getMemberIds()));
        }
        if (!CollectionUtils.isEmpty(query.getStatuses())) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("status", query.getStatuses()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }
        if (Objects.nonNull(query.getLastLtTimeAfterThirtySixHour())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("lastLtTimeAfterThirtySixHour", query.getLastLtTimeAfterThirtySixHour()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder purchaseGoods(PurchaseGoodsQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getStart())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").gte(DateFormatUtils.format(query.getStart(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getEnd())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").lt(DateFormatUtils.format(query.getEnd(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getQuotationGoodsId())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("quotationGoodsId", query.getQuotationGoodsId()));
        }
        if (Objects.nonNull(query.getVendorId())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("vendorId", query.getVendorId()));
        }
        if (Objects.nonNull(query.getVmiGoods())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("vmiGoods", query.getVmiGoods()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder bindingInventedVendor(BindingInventedVendorQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getVendorId())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("organizationId", query.getVendorId()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("del", query.getDel()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder member(MemberQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getProtocolAccount())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("protocolAccount", query.getProtocolAccount()));
        }
        if (!CollectionUtils.isEmpty(query.getIds())) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("id", query.getIds()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder vendorOrder(VendorOrderQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getStart())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").gte(DateFormatUtils.format(query.getStart(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getEnd())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").lt(DateFormatUtils.format(query.getEnd(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getVendorId())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("vendorId", query.getVendorId()));
        }
        if (!CollectionUtils.isEmpty(query.getStatus())) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("status", query.getStatus()));
        }
        if (Objects.nonNull(query.getLastLtTimeAfterTwelveHour())) {
            BoolQueryBuilder subBoolQueryBuilder1 = QueryBuilders.boolQuery();
            subBoolQueryBuilder1.must(QueryBuilders.termsQuery("status", Arrays.asList(3, 4, 5)));
            subBoolQueryBuilder1.must(QueryBuilders.matchQuery("ordersTimeGtTimeAfterTwelveHour", true));

            BoolQueryBuilder subBoolQueryBuilder2 = QueryBuilders.boolQuery();
            subBoolQueryBuilder2.must(QueryBuilders.termsQuery("status", Arrays.asList(1, 2)));
            // now() > ( tvo.TIME + INTERVAL '12 H' )
            subBoolQueryBuilder2.must(QueryBuilders.rangeQuery("time").lt(DateTimeFormatter.ofPattern(DateUtils.FORMAT_PATTERN_DATETIME).format(LocalDateTime.now().minusHours(12L))));

            BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
            queryBuilder.should(subBoolQueryBuilder1);
            queryBuilder.should(subBoolQueryBuilder2);
            queryBuilder.minimumShouldMatch(1);
            boolQueryBuilder.must(queryBuilder);
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder orderGoods(List<Long> vendorOrderId) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (!CollectionUtils.isEmpty(vendorOrderId)) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("vendorOrderId", vendorOrderId));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder goods(GoodsQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getStart())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").gte(DateFormatUtils.format(query.getStart(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getEnd())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").lt(DateFormatUtils.format(query.getEnd(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getVendorId())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("vendorId", query.getVendorId()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder goodsProfile(GoodsProfileQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (!CollectionUtils.isEmpty(query.getIds())) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("id", query.getIds()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder orderGoodsRefund(OrderGoodsRefundQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(query.getStart())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").gte(DateFormatUtils.format(query.getStart(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getEnd())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").lt(DateFormatUtils.format(query.getEnd(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getVendorId())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("vendorId", query.getVendorId()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }
        return boolQueryBuilder;
    }

    public static BoolQueryBuilder platformGrantUser(Long vendorId) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(vendorId)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("organizationId", vendorId));
        }
        boolQueryBuilder.must(QueryBuilders.matchQuery("del", false));
        return boolQueryBuilder;
    }

    /**
     * 询价查询 查询未被参与的所有询价商品
     * @param query
     * @return
     */
    public static BoolQueryBuilder inquiryGoods(InquiryGoodsQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if (Objects.nonNull(query.getStart())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").gte(DateFormatUtils.format(query.getStart(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getEnd())) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("time").lt(DateFormatUtils.format(query.getEnd(), "yyyy-MM-dd HH:mm:ss")));
        }
        if (Objects.nonNull(query.getPartakeCount())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("partakeCount", query.getPartakeCount()));
        }
        if (Objects.nonNull(query.getQuoteCount())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("quoteCount", query.getQuoteCount()));
        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("del", query.getDel()));
        }

        return boolQueryBuilder;
    }

    /**
     * 澄清记录查询
     * @param query
     * @return
     */
    public static BoolQueryBuilder inquiryGoodsClarify(InquiryGoodsClarifyQuery query) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if (Objects.nonNull(query.getVendorId())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("inquiryGoodsId",query.getInquiryGoodsId()));

        }
        if (Objects.nonNull(query.getDel())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("del", query.getDel()));
        }
        if (Objects.nonNull(query.getVendorId())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("vendorId",query.getVendorId()));
        }

        return boolQueryBuilder;
    }
}
